import Flutter
import QMapKit
import AMapNaviKit
import AMapSearchKit
import AMapLocationKit
import UIKit

public class T3MapPlugin: NSObject, FlutterPlugin {
    public static func register(with registrar: FlutterPluginRegistrar) {

        //高德： 648f91b3a38ba6f480dba6d05df709ff
        //腾讯：QMBBZ-IFDHR-TQMW7-WTNNY-XOFDF-PXF5F

        AMapLocationManager.updatePrivacyShow(.didShow, privacyInfo: .didContain)
        AMapLocationManager.updatePrivacyAgree(.didAgree)

        AMapSearchAPI.updatePrivacyShow(.didShow, privacyInfo: .didContain)
        AMapSearchAPI.updatePrivacyAgree(.didAgree)

        MAMapView.updatePrivacyShow(.didShow, privacyInfo: .didContain)
        MAMapView.updatePrivacyAgree(.didAgree)
        AMapServices.shared().apiKey = "648f91b3a38ba6f480dba6d05df709ff"
        print("【多地图】地图启动-4，[高德设备标识]: AMapServices.shared().identifier = \(AMapServices.shared()?.identifier ?? "")")


        QMapServices.shared().apiKey = "QMBBZ-IFDHR-TQMW7-WTNNY-XOFDF-PXF5F"
        QMSSearchServices.shared().apiKey = "QMBBZ-IFDHR-TQMW7-WTNNY-XOFDF-PXF5F"
        QMSSearchServices.shared().secretKey = "p8O5bHSHt5OEiMyvrsEfhOkHNB0r36ia"
//        TMMServices.shared().apiKey = "QMBBZ-IFDHR-TQMW7-WTNNY-XOFDF-PXF5F"
//        TMMServices.shared().secretKey = "p8O5bHSHt5OEiMyvrsEfhOkHNB0r36ia"

        if QMapServices.shared().apiKey.isEmpty || QMSSearchServices.shared().apiKey.isEmpty {
            print("【多地图】地图启动-4，腾讯地图启动失败")
        }

        // 隐私协议开关，since 4.5.6
        QMapServices.shared().setPrivacyAgreement(true)

        let channel = FlutterMethodChannel(name: "t3_map", binaryMessenger: registrar.messenger())
        let instance = T3MapPlugin()

        let addressInstance = T3SelectAddressTool.shared
        addressInstance.channel = channel

        let mapFactory = T3FMapViewPlatformViewFactory(registrar: registrar)

        registrar.register(mapFactory, withId: "com.t3.map.flutter.mapview")

        registrar.addMethodCallDelegate(instance, channel: channel)
    }

    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        switch call.method {
        case "getPlatformVersion":
            result("iOS " + UIDevice.current.systemVersion)
        case "walkingRoute#calculate":
            T3FRouteManager.walkRoute(arguments: call.arguments, result: result)
        case "driveRoute#calculate":
            T3FRouteManager.driveRoute(arguments: call.arguments, result: result)
        case "poi#regeo":
            T3FPOIManager.regeo(arguments: call.arguments, result: result)
        default:
            result(FlutterMethodNotImplemented)
        }
    }
    
    public static func handleFlutterAddressSelect(params: [String: Any], callBack: (([String : Any]?)-> Void)?) {
        T3SelectAddressTool.shared.callSelectAddress(params: params, callBack: callBack)
    }
}
